package com.ihrm.system.controller;

import com.ihrm.common.controller.BaseController;
import com.ihrm.common.entity.PageResult;
import com.ihrm.common.entity.Result;
import com.ihrm.common.entity.ResultCode;
import com.ihrm.domain.system.Role;
import com.ihrm.domain.system.response.RoleResult;
import com.ihrm.system.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @author 小伟
 * @version V1.0
 * @Package com.ihrm.system.controller
 * @date 2020/12/13 9:39
 * @Copyright © 2020-2021 郑州恒星科技有限责任公司
 */
@RestController
@RequestMapping("/sys")
public class RoleController extends BaseController {
    @Autowired
    RoleService roleService;
    /**
     * 新增企业权限
     *
     * @param role 权限信息
     * @return 添加结果
     */
    @PostMapping("/role")
    public Result addRole(@RequestBody Role role) {
        role.setCompanyId(companyId);
        roleService.saveRole(role);
        return new Result(ResultCode.SUCCESS);
    }

    /**
     * 修改企业权限信息
     *
     * @param id         权限id
     * @param role 修改信息
     * @return 修改结果
     */
    @PutMapping("/role/{id}")
    public Result updateRole(@PathVariable String id, @RequestBody Role role) {
        role.setId(id);
        roleService.updateRole(role);
        return new Result(ResultCode.SUCCESS);
    }

    /**
     * 根据id查询权限信息
     *
     * @param id 权限id
     * @return 查询结果
     */
    @GetMapping("/role/{id}")
    public Result findById(@PathVariable String id) {
        Role role = roleService.findById(id);
        RoleResult roleResult = new RoleResult(role);
        return new Result(ResultCode.SUCCESS, roleResult);
    }

    /**
     * 根据id删除权限信息
     *
     * @param id 权限id
     * @return 删除结果
     */
    @DeleteMapping("/role/{id}")
    public Result deleteById(@PathVariable String id) {
        roleService.deleteById(id);
        return new Result(ResultCode.SUCCESS);
    }

    /**
     * 根据企业id查询全部权限信息
     * @param page 页码
     * @param size 页面大小
     * @return 查询结果
     */
    @GetMapping("/role")
    public Result findAll(int page,int size) {
        Page rolePage = roleService.findSearch(companyId,page, size);
        PageResult<Role> pageResult = new PageResult<Role>(rolePage.getTotalElements(), rolePage.getContent());
        return new Result(ResultCode.SUCCESS, pageResult);
    }
    /**
     * 根据企业id查询全部权限信息
     * @return 查询结果
     */
    @GetMapping("/role/list")
    public Result findAll() {
        List<Role> roles = roleService.findAll(companyId);
        return new Result(ResultCode.SUCCESS, roles);
    }

    @PutMapping("/role/assignPerms")
    public Result assignPerms(@RequestBody Map<String,Object> map) {
        String roleId = (String) map.get("id");
        List<String> permIds =(List<String>) map.get("permIds");
        roleService.assignPerms(roleId,permIds);
        return new Result(ResultCode.SUCCESS);
    }
}
